package cn.zwx.member.svc.utils;

import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author zhangwenxue
 */
public class GenerateStrUtil {
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int LENGTH = 10;

    public static String generatePassword(Integer length) {
        if (ObjectUtils.isEmpty(length)){
            length = LENGTH;
        }
        StringBuilder password = new StringBuilder(length);
        final ThreadLocalRandom current = ThreadLocalRandom.current();
        for (int i = 0; i < length; i++) {
            int index = current.nextInt(CHARACTERS.length());
            char character = CHARACTERS.charAt(index);
            password.append(character);
        }
        return password.toString();
    }
}
